<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8" %>
<%@page import="java.util.UUID" %>
<%@page import="java.security.interfaces.RSAPublicKey" %>
<%@page import="org.apache.commons.lang.ArrayUtils" %>
<%@page import="org.apache.commons.codec.binary.Base64" %>
<%@page import="org.apache.shiro.web.filter.authc.FormAuthenticationFilter" %>
<%@page import="org.springframework.context.ApplicationContext" %>
<%@page import="com.hboxs.asl.Setting" %>
<%@page import="com.hboxs.asl.util.SettingUtils" %>
<%@page import="com.hboxs.asl.util.SpringUtils" %>
<%@page import="com.hboxs.asl.Setting.CaptchaType" %>
<%@page import="com.hboxs.asl.Setting.AccountLockType" %>
<%@page import="com.hboxs.asl.service.RSAService" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%
    String base = request.getContextPath();
    String captchaId = UUID.randomUUID().toString();
    ApplicationContext applicationContext = SpringUtils.getApplicationContext();
    Setting setting = SettingUtils.get();
    if (applicationContext != null) {
%>
<shiro:authenticated>
    <%
        response.sendRedirect(base + "/asl-admin/common/main.htm");
    %>
</shiro:authenticated>
<%
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <%
        if (applicationContext != null) {
            RSAService rsaService = SpringUtils.getBean("rsaServiceImpl", RSAService.class);
            RSAPublicKey publicKey = rsaService.generateKey(request);
            String modulus = Base64.encodeBase64String(publicKey.getModulus().toByteArray());
            String exponent = Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray());

            String message = null;
            String loginFailure = (String) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
            if (loginFailure != null) {
                if (loginFailure.equals("org.apache.shiro.authc.pam.UnsupportedTokenException")) {
                    message = "admin.captcha.invalid";
                } else if (loginFailure.equals("org.apache.shiro.authc.UnknownAccountException")) {
                    message = "admin.login.unknownAccount";
                } else if (loginFailure.equals("org.apache.shiro.authc.DisabledAccountException")) {
                    message = "admin.login.disabledAccount";
                } else if (loginFailure.equals("org.apache.shiro.authc.LockedAccountException")) {
                    message = "admin.login.lockedAccount";
                } else if (loginFailure.equals("org.apache.shiro.authc.IncorrectCredentialsException")) {
                    if (ArrayUtils.contains(setting.getAccountLockTypes(), AccountLockType.admin)) {
                        message = "admin.login.accountLockCount";
                    } else {
                        message = "admin.login.incorrectCredentials";
                    }
                } else if (loginFailure.equals("org.apache.shiro.authc.AuthenticationException")) {
                    message = "admin.login.authentication";
                }
            }
    %>
    <title>易经教育系统 | 管理中心</title>
    <meta http-equiv="expires" content="0"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>


    <script type="text/javascript" src="<%=base%>/resources/admin/js/jquery.js"></script>
    <script type="text/javascript" src="<%=base%>/resources/admin/js/jsbn.js"></script>
    <script type="text/javascript" src="<%=base%>/resources/admin/js/prng4.js"></script>
    <script type="text/javascript" src="<%=base%>/resources/admin/js/rng.js"></script>
    <script type="text/javascript" src="<%=base%>/resources/admin/js/rsa.js"></script>
    <script type="text/javascript" src="<%=base%>/resources/admin/js/base64.js"></script>
    <script type="text/javascript" src="<%=base%>/resources/admin/js/common.js"></script>
    <script type="text/javascript">
        $().ready(function () {

            var $loginForm = $("#loginForm");
            var $enPassword = $("#enPassword");
            var $username = $("#username");
            var $password = $("#password");
            var $captcha = $("#captcha");
            var $captchaImage = $("#captchaImage");
            var $isRememberUsername = $("#isRememberUsername");

            // 记住用户名
            if (getCookie("adminUsername") != null) {
                $isRememberUsername.prop("checked", true);
                $username.val(getCookie("adminUsername"));
                $password.focus();
            } else {
                $isRememberUsername.prop("checked", false);
                $username.focus();
            }

            // 更换验证码
            $captchaImage.click(function () {
                $captchaImage.attr("src", "<%=base%>/asl-admin/common/captcha.htm?captchaId=<%=captchaId%>&timestamp=" + (new Date()).valueOf());
            });

            // 表单验证、记住用户名
            $loginForm.submit(function () {
                if ($username.val() == "") {
                    $.message("warn", "<%=SpringUtils.getMessage("admin.login.usernameRequired")%>");
                    return false;
                }
                if ($password.val() == "") {
                    $.message("warn", "<%=SpringUtils.getMessage("admin.login.passwordRequired")%>");
                    return false;
                }
                if ($captcha.val() == "") {
                    $.message("warn", "<%=SpringUtils.getMessage("admin.login.captchaRequired")%>");
                    return false;
                }

                if ($isRememberUsername.prop("checked")) {
                    addCookie("adminUsername", $username.val(), {expires: 7 * 24 * 60 * 60});
                } else {
                    removeCookie("adminUsername");
                }

                var rsaKey = new RSAKey();
                rsaKey.setPublic(b64tohex("<%=modulus%>"), b64tohex("<%=exponent%>"));
                var enPassword = hex2b64(rsaKey.encrypt($password.val()));
                $enPassword.val(enPassword);
            });

            <%if (message != null) {%>
            $.message("error", "<%=SpringUtils.getMessage(message, setting.getAccountLockCount())%>");
            <%}%>
        });
    </script>
    <%} else {%>
    <title>提示信息</title>
    <meta http-equiv="expires" content="0"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>
    <%}%>
    <link href="<%=base%>/resources/admin/css/login.css" rel="stylesheet" type="text/css"/>
    <link href="<%=base%>/resources/admin/css/common.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<!-- contact-form -->
<div class="message warning">
    <div class="contact-form">
        <div class="logo">
            <h1><%=SettingUtils.get().getSiteName()%>系统管理中心</h1>
            <h3>总 后 台</h3>
        </div>
        <!--- form --->
        <form id="loginForm" action="login.jsp" method="post" class="form">
            <input type="hidden" id="enPassword" name="enPassword"/>
            <%if (ArrayUtils.contains(setting.getCaptchaTypes(), CaptchaType.adminLogin)) {%>
            <input type="hidden" name="captchaId" value="<%=captchaId%>"/>
            <%}%>
            <ul>
                <li>
                    <label>账号</label>
                    <input type="text" id="username" name="username"
                           placeholder="<%=SpringUtils.getMessage("admin.login.username")%>" value="" required/>
                </li>
                <li>
                    <label>密码</label>
                    <input type="password" id="password" name="password" autocomplete="off"
                           placeholder="<%=SpringUtils.getMessage("admin.login.password")%>" value="" required/>
                </li>
                <%if (ArrayUtils.contains(setting.getCaptchaTypes(), CaptchaType.adminLogin)) {%>
                <li>
                    <label><img src="/resources/admin/images/admin-login/lock.png" alt=""></label>
                    <input type="text" id="captcha" name="captcha" maxlength="4"
                           placeholder="<%=SpringUtils.getMessage("admin.captcha.name")%>" style="width:39%"
                           value="" required/>
                    <img id="captchaImage" class="captchaImage"
                         src="<%=base%>/asl-admin/common/captcha.htm?captchaId=<%=captchaId%>"
                         title="<%=SpringUtils.getMessage("admin.captcha.imageTitle")%>"/>
                </li>
                <%}%>
                <p><a href="#"><input type="checkbox" id="isRememberUsername"
                                      value="true"/><%=SpringUtils.getMessage("admin.login.rememberUsername")%>
                </a></p>
                <li class="style">
                    <input type="submit" class="loginButton" value="<%=SpringUtils.getMessage("admin.login.login")%>"/>
                </li>
            </ul>
            <div class="clear"></div>
        </form>
    </div>
</div>
</body>
</html>